<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Model extends CI_Model {
  

	public $table="";
	public $a = "a"; //alias of this table
	public $object = "";
	
	public function insert_record($obj)
	{
		if($this->db->insert($this->table, $obj))
		{
			$data['success']="Added successfully";
		}
		else
		{
			$data['error'] = $this->util->handle_error();
		}
		return $data;
	}
	
	public function update_record($obj, $where)
	{
		if($this->db->update($this->table, $obj, $where))
		{
			$data['success']="Updated successfully";
		}
		else
		{
			$data['error'] = $this->util->handle_error();
		}
		return $data;
	}

	public function save($where)
	{
        $obj_name = $this->object;
		$obj = new $obj_name();
		$obj->set_vars();

        //the current value
        if(current($where))
		{
			return $this->update_record($obj, $where);
		}
		else
		{
			return $this->insert_record($obj);
		}
	}
	
	public function delete_record($where)
	{
		if( $this->db->delete($this->table, $where) )
		{
			$data['success']= "Deleted successfully";
		}
		else
		{
			$data['error'] = $this->util->handle_error();
		}
		return $data;
	}	

	public function get_record($where)
	{

    	if($where && current($where))
		{
			$query = $this->db->get_where($this->table, $where);
			if($query->num_rows()>0)
				return $query->row();
			else  redirect("errors/page_missing")  ;
		}
		else
		{
            $obj_name = $this->object;
			return new $obj_name();
		}
	}

	public function activate($where, $field="active")
	{
        $key = key($where);
		$sql="UPDATE $this->table SET $field=1-$field WHERE $key=?";
		if(! $this->db->query($sql, $where[$key])){
         	return $this->util->handle_error();
	    }
			return 1;

	}

	public function get_all($offset=0, $override = false, $items_per_page=200)
	{
        $sort_col = $this->input->post("sort_col");
		$sort_dir = $this->input->post("sort_dir");

		if( ! $override)
	   	   $this->db->select();
        $this->db->from($this->table . " " . $this->a);
		if($sort_col)
		{
			$this->db->order_by($sort_col, $sort_dir);
		}
		$this->db->limit($items_per_page, $offset);
		$query = $this->db->get();
		//echo $this->db->last_query();
		return $query->result();
	}

	public function get_options($table, $value, $text, $default_text="", $options="", $order_by="")
	{
		$this->db->select("$value, $text");
		$this->db->from($table);
		if($options)
	   		$this->db->where($options);
		if($order_by)
           $this->db->order_by($order_by);
		else
			$this->db->order_by($text);
		$query = $this->db->get();


	    if($default_text)
			$result= array(""=>  " -- " . $default_text . " -- ");
		$rows = $query->result();
		foreach($rows as $row)
		{
			$result[$row->$value] = $row->$text;
		}
		return $result;
	}

	function insert_rows($column_names, $rows, $escape = true)
    {
    	$table_name = $this->table;
        /* Build a list of column names */
        $columns    = array_walk($column_names, array($this, 'prepare_column_name') );
        $columns    = implode(',', $column_names);

        /* Escape each value of the array for insertion into the SQL string */
        if( $escape ) array_walk_recursive( $rows, array( $this, 'escape_value' ) );

        /* Collapse each rows of values into a single string */
        $length = count($rows);
        for($i = 0; $i < $length; $i++) $rows[$i] = implode(',', $rows[$i]);

        /* Collapse all the rows into something that looks like
         *  (r1_val_1, r1_val_2, ..., r1_val_n),
         *  (r2_val_1, r2_val_2, ..., r2_val_n),
         *  ...
         *  (rx_val_1, rx_val_2, ..., rx_val_n)
         * Stored in $values
         */
        $values = "(" . implode( '),(', $rows ) . ")";

        $sql = "INSERT INTO $table_name ( $columns ) VALUES $values";

        if($this->db->simple_query($sql))
        {
			$data['success']= "Saved successfully";
		}
		else
		{
			$data['error'] = $this->util->handle_error();
		}
		return $data;


    }

    function escape_value(& $value)
    {
        if( is_string($value) )
        {
            $value = "'" . mysql_real_escape_string($value) . "'";
        }
    }

    function prepare_column_name(& $name)
    {
        $name = "`$name`";
    }

	
}